package genericdemo;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * @Author 12629
 * @Description：
 */
class MyArray<T > {
    public T[] array = (T[])new Object[10];//1
    //public T[] array = new T[10];
    // 1-->  public Object[] array = new Object[10];-> 会存在一个Object的数组

    public T getPos(int pos) {
        return this.array[pos];
    }
    public void setVal(int pos,T val) {
        this.array[pos] = val;
    }

    public T[] getArray() {
        return array;
    }

    /*
    public Object[] getArray() {
        return array;
    }
     */
}
class MyArray2<T extends Number> {
    public T[] array;

    public MyArray2() {

    }

    /**
     * 通过反射创建，指定类型的数组
     * @param clazz
     * @param capacity
     */
    public MyArray2(Class<T> clazz, int capacity) {
        array = (T[]) Array.newInstance(clazz, capacity);
    }

    public T getPos(int pos) {
        return this.array[pos];
    }
    public void setVal(int pos,T val) {
        this.array[pos] = val;
    }
    public T[] getArray() {
        return array;
    }
}




public class TestDemo {

    public static void main(String[] args) {
        MyArray<Integer> myArray = new MyArray<>();//2
        System.out.println(myArray);
        MyArray<String> myArray2 = new MyArray<>();//2
        System.out.println(myArray2);
    }



    public static void main4(String[] args) {
        MyArray2<Integer> myArray2 = new MyArray2<>(Integer.class,10);
        myArray2.setVal(0,10);
        Integer[] tmp = myArray2.getArray();
        System.out.println(Arrays.toString(tmp));
    }

    public static void main3(String[] args) {
        MyArray<Integer> myArray = new MyArray<>();//2
        myArray.setVal(0,10);
        Integer[] tmp = myArray.getArray();
        System.out.println(Arrays.toString(tmp));
    }

    /*public static void main2(String[] args) {
        genericdemo.MyArray myArray = new genericdemo.MyArray();//2
        myArray.setVal(0,10);
        myArray.setVal(1,"hello");

    }
    public static void main1(String[] args) {
        genericdemo.MyArray<Integer> myArray = new genericdemo.MyArray<>();//2
        myArray.setVal(0,10);

        //myArray.setVal(1,"hello");

        int ret = myArray.getPos(1);//3

        System.out.println(ret);

        genericdemo.MyArray<String> myArray2 = new genericdemo.MyArray<>();//2
        myArray2.setVal(0,"hello");
        myArray2.setVal(1,"abc");

    }*/
}
